package com.xinli.xinlifm.view.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.CategoryBean;

import java.util.List;

/**
 * created by javac on 16-10-13.
 * email:kylin_javac@outlook.com
 * desc:
 */

public class CategoryAdapter extends BaseAdapter {
    private List<CategoryBean.DataBean> mDList;
    private Context mContext;
    public boolean mBoolean;

    public CategoryAdapter(List<CategoryBean.DataBean> DList, Context context) {
        mDList = DList;
        mContext = context;
    }

    public CategoryAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return mDList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        CategoryBean.DataBean dataBean = mDList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item, null);
            holder = new ViewHolder();
            holder.categoryAuthor = (TextView) convertView.findViewById(R.id.category_author);
            holder.categoryContent = (TextView) convertView.findViewById(R.id.category_content);
            holder.categoryNumber = (TextView) convertView.findViewById(R.id.category_number);
            holder.categoryImg = (ImageView) convertView.findViewById(R.id.category_img);
            holder.category_Rll = (RelativeLayout) convertView.findViewById(R.id.category_Rll);
            holder.category_Rll.setOnClickListener(dataBean);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        holder.categoryContent.setText(dataBean.getTitle());
        holder.categoryAuthor.setText(dataBean.getSpeak());
        holder.categoryNumber.setText("收听:" + dataBean.getFavnum());
        Picasso.with(mContext).load(dataBean.getCover()).into(holder.categoryImg);
        return convertView;
    }

    static class ViewHolder {
        private LinearLayout categoryLin3;
        private TextView categoryContent;
        private TextView categoryAuthor;
        private TextView categoryNumber;
        private RelativeLayout category_Rll;
        private ImageView categoryImg;
    }

    public void addData(List<CategoryBean.DataBean> dataBean) {
//        boolean contains = mDList.contains(dataBean);
//        mBoolean = mDList.containsAll(dataBean);
        if (dataBean != null) {
            mDList.addAll(mDList.size(), dataBean);
            int size = mDList.size();
            Log.e("CategoryAdapter", "addData: " + size);
        }
    }
}
